package org.myy.learn.hot.链表.删除链表的倒数第N个节点;


public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1, head); //ToDO 亚节点作用：统一删除操作[首节点][前一节点]
        ListNode fast = head;
        ListNode slow = dummy;
        for (int i=0; i<n; i++){
            if (fast == null){return null;}
            fast = fast.next;
        }
        while (fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;//删除节点
        return dummy.next;
    }
//    public ListNode removeNthFromEnd(ListNode head, int n) {
//        ListNode fast = new ListNode(-1, head);
//        ListNode slow = new ListNode(-1, head);
//        for (int i=0; i<n; i++){
//            if (fast == null){return null;}
//            fast = fast.next;
//        }
//        while (fast != null){
//            fast = fast.next;
//            slow = slow.next;
//        }
//        //删除节点
//        slow.next = fast.next.next;
//        return head;
//    }
}
